
$().ready(function() {
	validateRule();

	var ue = UE.getEditor('content');
	$('#content').data('editor', ue);
	$('#content').parents('form').submit(function () {
		if (ue.queryCommandState('source')) {
			ue.execCommand('source');
		}
	});

	UE.registerUI('uploadImage', function (editor, uiName) {
		return new UE.ui.Button({
			name: '上传图片',
			title: '上传图片',
			cssRules: 'background-position:-380px 0',
			onclick: function () {
				layer.open({
					id: 'selectImgs',
					type: 2,
					title: '批量选择图片',
					maxmin: true,
					shadeClose: false,
					area: ['60%', '70%'],
					content: '/platform/config/batchUploadImgUeditor'  // iframe的url
				});
			}
		});
	});

});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {
	var obj = $('#signupForm').serialize();
	var content = UE.getEditor('content').getContent();
	obj.content = content;
	$.ajax({
		cache : true,
		type : "POST",
		url : "/mall/notice/save",
		data : obj,// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				if (data.msg) {
					parent.layer.alert(data.msg)
				}else {
					window.location.href = "/";
				}
			}

		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			title : {
				required : true
			},
			content : {
				required : true
			}
		},
		messages : {
			title : {
				required : icon + "公告标题不能为空"
			},
			content : {
				required : icon + "公告图片不能为空"
			}
		}
	})
}

// 加载图片进ueditor中
function loadImgsToUeditor(result) {
	if (!result || result.length < 1) {
		parent.layer.msg("上传图片失败!", {icon: 7});
		return;
	}
	console.info(result);
	/* var imgUrlArray = result.split(","); */
	let editor = UE.getEditor('content');
	for (var i = 0; i < result.length; i++) {
		var imgUrl = result[i];
		editor.execCommand('inserthtml', `<img src=${imgUrl}>`);
	}
}
// ueditor 批量上传图片
function selectImgsToUeditor() {
	layer.open({
		id:'selectImgsToUeditor',
		type : 2,
		title : '批量选择图片',
		maxmin : true,
		shadeClose : false,
		area : [ '60%', '70%' ],
		content : '/platform/config/batchUploadImg/' // iframe的url
	});
}